<template>

  <div class="login">
    <div style="width:33%; ">
      <div style="width:100%;height:39%;display: flex;flex-direction: column; margin-top: 200px;">
        <div style="width:100%;color: #09dfeb;text-align: center;font-size: 30px;">
          昌吉回族自治州机动车监管平台
        </div>
        <div style="width:100%;height:80%;display:flex;margin-top: 50px;background-color:  rgba(14, 45, 77, 1);">
          <div style="width:5%;height:100%;">
            <div
              style="width:100%;height:5%;border-top:2px solid rgba(9, 223, 235, 1.0);border-left:2px solid rgba(9, 223, 235, 1.0);">
            </div>
            <div style="width:100%;height:90%;border-left:1px solid rgba(9, 223, 235, 0.2);">
            </div>
            <div
              style="width:100%;height:5%;border-bottom:2px solid rgba(9, 223, 235, 1.0);border-left:2px solid rgba(9, 223, 235, 1.0);">
            </div>
          </div>
          <div style="flex:1;height:100%;">
            <div style="width:100%;height:5%;border-top:1px solid rgba(9, 223, 235, 0.2);">
            </div>
            <div style="width:100%;height:90%;">
              <!-- 此处是控件主体 -->
              <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
                <el-form-item prop="userName">
                  <el-input v-model="loginForm.userName" type="text" auto-complete="off" placeholder="账号">
                    <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
                  </el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
                    @keyup.enter.native="handleLogin">
                    <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
                  </el-input>
                </el-form-item>
                <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
                <el-form-item style="width:100%;">
                  <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                    @click.native.prevent="handleLogin">
                    <span v-if="!loading">登 录</span>
                    <span v-else>登 录 中...</span>
                  </el-button>
                </el-form-item>
              </el-form>
              <!-- 此处是控件主体 -->
            </div>
            <div style="width:100%;height:5%;border-bottom:1px solid rgba(9, 223, 235, 0.2);">
            </div>
          </div>
          <div style="width:5%;height:100%;">
            <div
              style="width:100%;height:5%;border-top:2px solid rgba(9, 223, 235, 1.0);border-right:2px solid rgba(9, 223, 235, 1.0);">
            </div>
            <div style="width:100%;height:90%;border-right:1px solid rgba(9, 223, 235, 0.2);">
            </div>
            <div
              style="width:100%;height:5%;border-bottom:2px solid rgba(9, 223, 235, 1.0);border-right:2px solid rgba(9, 223, 235, 1.0);">
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="el-login-footer" style="color: #00aaff;">
      <span>Copyright © 2021-2025 天津市生态环境科学研究院 版权所有 </span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { getCaptcha, getMyToken } from "@/api/largescreen";
import { getToken, setToken, removeToken } from '@/utils/auth'

export default {
  name: "Login",
  data() {
    return {
      captchaUrl: undefined,
      codeUrl: "",
      cookiePassword: "",
      grant_type: 'password',
      platId: 1,
      loginForm: {
        userName: "",
        password: "",
        rememberMe: false,
        token: "",
      },
      loginRules: {
        userName: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        validateCode: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        //code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {

        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    // this.getValidateCode();
    this.getCookie();
  },
  methods: {
    GetMyToken() {
      getMyToken({
        username: this.loginForm.userName,
        password: this.loginForm.password,
        grant_type: this.grant_type,
      }).then(res => {

        const { access_token, token_type, refresh_token } = res.data
        let storage = window.localStorage;
        let token = token_type + " " + access_token
        setToken('hezuoMJXT', token);

        storage["token_type"] = res.data.token_type;
        storage["access_token"] = res.data.access_token;
        this.$router.push({ path: '/CompanyScreen' })

      }).catch(err => {
        this.loading = false;
        removeToken("hezuoMJXT")
        // this.getValidateCode()
      })
    },
    getValidateCode() {
      getCaptcha().then(response => {
        const { img, uuid } = response.data
        this.captchaUrl = "data:image/gif;base64," + img
        this.loginForm.uuid = uuid;
      })
    },
    getCookie() {
      console.log("===============getCookie")
      const userName = Cookies.get("userName");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        userName: userName === undefined ? this.loginForm.userName : userName,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("userName", this.loginForm.userName, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("userName");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.GetMyToken();
        }
      });
    }
  }
};
//22,26,18,13
</script>

<style rel="stylesheet/scss" lang="scss">

.login {
  display: flex;
  justify-content: center;
  // align-items: center;
  height: 100%;
  background-image: url("../assets/image/background.jpg");
  background-size: 100% 100%;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #09dfeb;
}

.login-form {
  border-radius: 6px;
  //background: #0e2d4d;
  opacity: 1;
  width: 100%;
  padding: 25px 25px 5px 25px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .el-input__inner {
    background-color: rgba(14, 45, 77, 1.0);
    border: 1px solid rgba(9, 223, 235, 1.0);
    color: white;
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #09dfeb;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.el-checkbox__inner {
  background-color: rgba(14, 45, 77, 0.8);
  border: 1px solid rgba(9, 223, 235, 1.0);
}
</style>
